package com.kfh.ybracelet.mqtt;

import android.content.Context;
import android.util.Log;

import com.kfh.ybracelet.mqttsource.IMqttDeliveryToken;
import com.kfh.ybracelet.mqttsource.MqttCallback;
import com.kfh.ybracelet.mqttsource.MqttMessage;
import com.kfh.ybracelet.utils.CommonUtils;
import com.kfh.ybracelet.utils.LogUtil;

/**
 * mqtt消息回调
 * Created by Aaron on 15/12/15.
 */
public class MqttMessageCallBack implements MqttCallback {
    //    private String instanceData = "";
    private Context ctx;
//    private SharedPreferences sp;

    public MqttMessageCallBack(Context ctx) {

        this.ctx = ctx;
//        sp = ctx.getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);

    }

    @Override
    public void connectionLost(Throwable throwable) {

        LogUtil.e("tag", "sorry 连接中断" + throwable.getMessage()
                + ", throwable:" + throwable.toString() + ",LocalizedMessage:" + throwable.getLocalizedMessage()
                + ", Cause :" + throwable.getCause().getMessage());


        throwable.printStackTrace();

        //网络不可用
        if (ctx != null && !CommonUtils.isNetworkAvailable(ctx)) {
            return;
        }

        //获取mqtt聊天服务器ip和端口,重新连接
        MqttManager.getInstance().getMqttIp(ctx);

    }

    //    int i = 0;
    @Override
    public void messageArrived(String topic, MqttMessage message) {

        //此线程为子线程
        Log.e("tag", "收到的消息:" + "topic:" + topic + ",消息内容:" + message.toString());

        if (CommonUtils.isNull(message.toString()))
            return;

        synchronized (this) {
//            i++;
//            Log.e("tag", " 开始处理 消息 : " + i);
            ChatMessageManager.getInstance().handlerMessage(topic, message.toString(), ctx);
//            Log.e("tag", " 消息处理完毕 : " + i);
        }

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

//        LogUtil.e("tag", getClass().getName() + " : deliveryComplete messageId " +
//                ": " + iMqttDeliveryToken.getMessageId());

    }

    /**
     * 收到了消息,先判断消息是否是系统消息,如果是系统消息, 则单独处理
     * <p/>
     * 然后再判断消息内容是否包含语音或者图片,如果包含语音或者图片,则
     * 先把语音和图片保存到本地,然后再插入数据库,
     * <p/>
     * //     * @param topic   topic
     * //     * @param message 消息
     *
     * @throws Exception
     */
//    @Override
//    public void messageArrived(String topic, MqttPublish message) throws Exception {
////        super.messageArrived(topic, message);
//
////        message.setDuplicate(true);
////        setPublishMessage(message);
//
////        message.
////        Mqtts
//
//
//        //此线程为子线程
//        LogUtil.e("tag", "收到的消息:" + "topic:" + topic + ",消息内容:" + message.getMessage()
//
////                        + ",\r\n" + "isRetryable : " + message.isRetryable()
//
//        );
//
////            LogUtil.e("tag", getClass().getName() + ", HomeActivty是否在顶端: " + isTopActivity());
//
//        ChatMessageManager.getInstance().handlerMessage(topic, message.getMessage().toString(), ctx);
//
//    }
//    private void getMqttService() {
//
//
//
//    }

}